package com.lyxy.controller;

import com.lyxy.pojo.Result;
import com.lyxy.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@Slf4j
public class UploadController {
    @Autowired
    private AliOSSUtils ossUtils;
    /*
     */

    /**
     * 文件上传到本地磁盘
     *
     * @param
     * @param
     * @param image
     * @return
     * @throws IOException
     *//*

    @PostMapping("/upload")
    public Result upload(String username, Integer age, MultipartFile image) throws IOException {
        log.info("username:{},age:{},file:{}", username, age, image);

//        获取文件原始文件名
        String originalFilename = image.getOriginalFilename();
        UUID uuid = UUID.randomUUID();
//        将文件保存到本地部署
        image.transferTo(new File("D:\\BaiduNetdiskDownload\\" + uuid
                + originalFilename.substring(originalFilename.lastIndexOf("."))));
        return Result.success();
    }

*/
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {
        log.info("file:{}", image);

        String upload = ossUtils.upload(image);
        return Result.success(upload);
    }

}
